package net.vinote.smart.platform.module.database;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.logging.Level;

import net.vinote.smart.platform.comm.PlatformConst;
import net.vinote.smart.platform.module.database.bo.BizObject;
import net.vinote.smart.platform.module.log.RunLogger;


/**
 * 将BizoOject中的值映射到将指定实体bean�?
 **/
public class BeanEntityFactory {
	private static final RunLogger logger = RunLogger.getLogger();
	private static BeanEntityFactory factory;

	private BeanEntityFactory() {
	}

	public static BeanEntityFactory getInstance() {
		if (factory == null) {
			synchronized ("") {
				if (factory == null) {
					factory = new BeanEntityFactory();
				}
			}
		}
		return factory;
	}

	public BizObject getObject(Entity entity) {
		BizObject bo = new BizObject();
		try {
			Class<?> clazz = entity.getClass();
			Field[] fields = clazz.getDeclaredFields();
			for (Field f : fields) {
				f.setAccessible(false);
				int modifiers = f.getModifiers();
				String name = f.getName();
				String type = f.getType().getSimpleName();
				if ((modifiers & Modifier.PRIVATE) > 0) {

					Object val = null;
					if (type.equals("boolean")) {
						if (name.startsWith("is")) {
							val = clazz.getMethod(name).invoke(entity);
						} else {
							val = clazz.getMethod(
									"is"
											+ String.valueOf(name.charAt(0))
													.toUpperCase()
											+ name.substring(1)).invoke(entity);
						}
					} else {
						val = clazz.getMethod(
								"get"
										+ String.valueOf(name.charAt(0))
												.toUpperCase()
										+ name.substring(1)).invoke(entity);
					}
					bo.setAttribute(name, val == null ? null : val.toString());

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bo;
	}

	public void getObject(Entity entity, BizObject bo) {
		try {
			// entity = clazz.newInstance();
			Field[] field = entity.getClass().getDeclaredFields();
			for (Field f : field) {
				int modifiers = f.getModifiers();
				String type = f.getType().getSimpleName();
				String name = f.getName();
				String value = bo.getAttribute(name);
				// 仅针对私有属性进行赋值
				if ((modifiers & Modifier.PRIVATE) > 0) {

					Method method = null;
					if ("boolean".equals(type)) {
						if (name.startsWith("is")) {
							method = entity.getClass().getMethod(
									"set" + name.substring(2), f.getType());
						} else {
							method = entity.getClass().getMethod(
									"set"
											+ String.valueOf(name.charAt(0))
													.toUpperCase()
											+ name.substring(1), f.getType());
						}
						method.invoke(entity, PlatformConst.YES.equals(value));
					} else {
						method = entity.getClass().getMethod(
								"set"
										+ String.valueOf(name.charAt(0))
												.toUpperCase()
										+ name.substring(1), f.getType());
						if ("int".equals(type)) {
							int result = 0;
							try {
								if (null != value && !"".equals(value.trim())) {
									result = Integer.parseInt(value);
								} else {
									result = PlatformConst.UN_KNOW;
								}
							} catch (Exception e) {
								result = PlatformConst.UN_KNOW;
								logger.log(
										Level.SEVERE,
										"类型转换错误:"
												+ bo.getAttribute(f.getName()),
										e);
							}
							method.invoke(entity, result);
						} else {
							method.invoke(entity, value);
						}
					}
				}
			}
		} catch (Exception e) {
			logger.log(Level.SEVERE, "装载Entity异常", e);
		}
	}
}
